using FSM.IDal; 
using FSM.IBll; 
using FSM.Model; 
using SqlSugar; 
using System; 
using System.Collections.Generic; 
using System.Text; 
 
namespace FSM.Bll 
{ 
    public class LogLoginBll: ILogLoginBll 
    { 
        #region 模块 
        private readonly ILogLoginDal dal; 
        public LogLoginBll(ILogLoginDal dal) 
        { 
            this.dal = dal; 
        } 
        #endregion 
 
        #region 查询 
 
        /// <summary> 
        /// 根据键值查询单个信息 
        /// </summary> 
        /// <param name="pkid">键值</param> 
        /// <returns></returns> 
        public log_login Get(int? pkid) 
        { 
            return dal.GetById(pkid); 
        }

        /// <summary> 
        /// 分页查询批量信息 
        /// </summary> 
        /// <param name="start_date">查询启始日期</param> 
        /// <param name="end_date">查询结束日期</param> 
        /// <param name="log_user">登录人員</param> 
        /// <param name="log_type">登录类型
        /// 1：網頁  
        /// 2：移動APP
        /// 3：Api</param> 
        /// <param name="log_terminal">登录设备
        /// 1：电脑
        /// 2：手机
        /// 3：Pad</param> 
        /// <param name="log_ip">登录IP</param> 
        /// <param name="page">页码</param> 
        /// <param name="page_size">每页显示的条数</param> 
        /// <returns></returns> 
        public Tuple<List<log_login>, int> ListPagin(DateTime? start_date, DateTime? end_date, string log_user, int? log_type, int? log_terminal, string log_ip, int page, int page_size)
        {
            ISugarQueryable<log_login> queryable = dal.CreateQueryable();
            queryable = queryable.WhereIF(start_date != null, p => p.log_date >= start_date.Value);
            queryable = queryable.WhereIF(end_date != null, p => p.log_date < end_date.Value.AddDays(1));
            queryable = queryable.WhereIF(!string.IsNullOrEmpty(log_user), p => p.log_user.Contains(log_user));
            queryable = queryable.WhereIF(log_type != null, p => p.log_type == log_type);
            queryable = queryable.WhereIF(log_terminal != null, p => p.log_terminal == log_terminal);
            queryable = queryable.WhereIF(!string.IsNullOrEmpty(log_ip), p => p.log_ip == log_ip);
            queryable = queryable.OrderBy("log_date desc");

            PageModel pm = new PageModel
            {
                PageIndex = page,
                PageSize = page_size
            };
            Tuple<List<log_login>, int> result = dal.GetPageListAndTotal(queryable, pm);
            return result;
        }

        #endregion

        #region 提交

        /// <summary>
        /// 新增日志信息
        /// </summary>
        /// <param name="model">日志实体</param>
        /// <returns></returns>
        public bool Insert(log_login model)
        {
            model.log_date = DateTime.Now;
            return dal.Insert(model);
        }

        #endregion
    }
} 
